package it.avantsoft.ls.gl.command;

import it.avantsoft.ls.gl.GroupChildHandler;

public class ConnectCommand extends GroupChildCommand {
    private String childId;
    private int listeningPort;

    public ConnectCommand(GroupChildHandler groupChildHandler, String childId, int listeningPort) {
        super(groupChildHandler);
        this.childId = childId;
        this.listeningPort = listeningPort;
    }

    public void execute() {
        GroupChildHandler child = getChild();
        String oldChildId = child.getChildId();
        child.setChildId(childId);
        child.setListeningPort(listeningPort);

        if (child.connect(oldChildId)) {
            /*child.signalSon(childId);
            Collection<GroupChildHandler> groupChildHandlers = child.getLabHandlers(childId);
            if (groupChildHandlers.size() != 0)
                for (GroupChildHandler groupChildHandler : groupChildHandlers) {
                    groupChildHandler.signalBrother(childId, listeningPort);
                }*/
        }
    }

    public String toString() {
        return "ConnectCommand{" +
                "child=" + getChild() +
                ", childId='" + childId + '\'' +
                ", listeningPort=" + listeningPort +
                '}';
    }
}
